package org.metawatch.manager;


import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;

public class _4911AndroidStorageService extends ListActivity {
	
	//NOTE: A lot of methods in this class are deprecated. Only a few are used to
	//handle storage of information. Display related methods and tasks are only used
	//for debugging purposes. Only methods pertaining to saving entries are public.
	//Debugging and testing methods are private
	
	private int currentTable = 1;
	public static final int GENERATE_ID = 1;
	public static final int CLEAR_ID = 2;
	public static final int VIEWTIMER_ID = 3;
	public static final int VIEWPEDOMETER_ID = 4;
	public static final int VIEWSESSION_ID = 5;
	
    
	/**
	 * Instance of database to manipulate
	 */
	private _4911MEADbAdapter mDbHelper;
	
	/**
	 * Constructor sets up the database instance and opens it
	 * @param ctx
	 */
	public _4911AndroidStorageService(Context ctx){
		 mDbHelper = new _4911MEADbAdapter(ctx);
	     mDbHelper.open();
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mDbHelper = new _4911MEADbAdapter(this);
        mDbHelper.open();
        drawScreen();
    }
    
    /**
     * Menu for debugging, but not used
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result;
        result = super.onCreateOptionsMenu(menu);
        menu.add(0, GENERATE_ID, 1, R.string.menu_generate_entry);
        menu.add(0, CLEAR_ID, 2, R.string.menu_clear_table);
        menu.add(0, VIEWTIMER_ID, 3, R.string.menu_show_timer);
        menu.add(0, VIEWPEDOMETER_ID, 4, R.string.menu_show_pedometer);
        menu.add(0, VIEWSESSION_ID, 5, R.string.menu_show_session);
        return result;
    }
    
    /**
     * Handles menu presses
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	boolean result;
    	int itemID = item.getItemId();
    	switch (itemID){
    	case GENERATE_ID:
    		createEntry();
    		return true;

    	case CLEAR_ID:
    		clearTable();
    		return true;

    	case VIEWTIMER_ID:
    		currentTable = 1;
    		drawScreen();
    		return true;

    	case VIEWPEDOMETER_ID:
    		currentTable = 2;
    		drawScreen();
    		return true;

    	case VIEWSESSION_ID:
    		currentTable = 3;
    		drawScreen();
    		return true;
    	}
    	
    	// wouldnt reach this code
        result = super.onOptionsItemSelected(item);
        return result;
    }
    
    /**
     * Creates table entries based on the type of table you want
     */
    private void createEntry(){
    	if (currentTable == 1){
    		createRandomTimerTime();
    	}
    	else if(currentTable == 2){
    		createRandomPedometerStep();
    	}
    	else if(currentTable == 3){
    		createRandomSessionSession();
    	}
    	else{
    		//never reaches here
    	}
    	drawScreen();
    }
    
    /**
     * Delete and recreate tables to clear them
     */
    private void clearTable(){
    	if (currentTable == 1){
    		mDbHelper.clearTimerTable();
    	}
    	else if(currentTable == 2){
    		mDbHelper.clearPedometerTable();
    	}
    	else if(currentTable == 3){
    		mDbHelper.clearSessionTable();
    	}
    	else{
    		//blow  up here
    	}
    	drawScreen();
    }
    
    /**
     * Handles screen rendering displaying database entries
     */
    private void drawScreen(){
    	if (currentTable == 1){
    		drawTimerScreen();
    	}
    	else if(currentTable == 2){
    		drawPedometerScreen();
    	}
    	else if(currentTable == 3){
    		drawSessionScreen();
    	}
    	else{
    		//blow  up here
    	}
    }
    
    /**
     * Method for testing and debug
     */
    private void createRandomTimerTime(){
    	
    	/* Debug stuff */
    	 // generate a random number between 0 and 24 for hours
    	double hhDouble = Math.random();
    	double tenhhDouble = hhDouble * 100;
    	int hhDirty = (int) tenhhDouble;
    	int hh = hhDirty % 24;
        
    	// generate a random number between 0 and 60 for mins
    	double mmDouble = Math.random();
    	double tenmmDouble = mmDouble * 100;
    	int mmDirty = (int) tenmmDouble;
    	int mm = mmDirty % 60;
    	
    	// generate a random number between 0 and 60 for secs
    	double ssDouble = Math.random();
    	double tenssDouble = ssDouble * 100;
    	int ssDirty = (int) tenssDouble;
    	int ss = ssDirty % 60;
    	
    	// generate a random number between 0 and 1000 for msec
    	double msDouble = Math.random();
    	double thousandmsDouble = msDouble * 1000;
    	int msDirty = (int) thousandmsDouble;
    	int ms = msDirty % 1000;
    	
    	saveTimerTime(hh, mm, ss, ms);
    	//String lapTime = hh + ":" + mm + ":" + ss + "." + ms;
    	
    	//mDbHelper.createTimerEntry(lapTime);
    }
    
    /**
     * Method to store a Timer entry into the database
     * @param hour will be between 0 to 23 inclusive
     * @param minute will be between 0 to 59 inclusive
     * @param second will be between 0 to 59 inclusive
     * @param millsecond will be 0 to 999 inclusive
     */
    public void saveTimerTime(int hour, int minute, int second, int millsecond){
    
    	int hourClean = Math.abs(hour) % 24;
    	int minuteClean = Math.abs(minute) % 60;
    	int secondClean = Math.abs(second) % 60;
    	int millsecondClean = Math.abs(millsecond) % 1000;
    	
    	String hh = "" + hourClean;
    	String mm = "" + minuteClean;
    	String ss = "" + secondClean;
    	String ms = "" + millsecondClean;
    	String lapTime = hh + ":" + mm + ":" + ss + "." + ms;
    	
    	mDbHelper.createTimerEntry(lapTime);
    }
    
    /**
     * Debug and testing method
     */
    private void createRandomPedometerStep(){
    	double hhDouble = Math.random();
    	double tenhhDouble = hhDouble * 10000;
    	int hhDirty = (int) tenhhDouble;
    	int steps = hhDirty;
    	savePedometerStep(steps);
    }
    
    /**
     * Saves a pedometer step into the database
     * @param steps will be between 0 and 60000
     */
    public void savePedometerStep(int steps){
    	int stepsClean = Math.abs(steps) % 60001;
    	mDbHelper.createPedometerEntry(stepsClean + "");
    }
    
    /**
     * Debug and testing method
     */
    private void createRandomSessionSession(){
	   	 // generate a random number between 0 and 24 for hours
	   	double hhDouble = Math.random();
	   	double tenhhDouble = hhDouble * 100;
	   	int hhDirty = (int) tenhhDouble;
	   	int hh = hhDirty % 24;
	       
	   	// generate a random number between 0 and 60 for mins
	   	double mmDouble = Math.random();
	   	double tenmmDouble = mmDouble * 100;
	   	int mmDirty = (int) tenmmDouble;
	   	int mm = mmDirty % 60;
	   	
	   	// generate a random number between 0 and 60 for secs
	   	double ssDouble = Math.random();
	   	double tenssDouble = ssDouble * 100;
	   	int ssDirty = (int) tenssDouble;
	   	int ss = ssDirty % 60;
	   	
	   	// generate a random number between 0 and 1000 for msec
	   	double msDouble = Math.random();
	   	double thousandmsDouble = msDouble * 1000;
	   	int msDirty = (int) thousandmsDouble;
	   	int ms = msDirty % 1000;
	   	String lapTime = hh + ":" + mm + ":" + ss + "." + ms;
	   	
    	double stepsRand = Math.random();
    	double stepsDouble = stepsRand * 10000;
    	int steps = (int) stepsDouble;
    	
    	
    	int sessionNo = (int)(Math.random() * 100);
    	
    	
    	createSessionSession(hh, mm, ss, ms, steps, sessionNo);
    }
    
    /**
     * Method to store a session in the database
     * @param hour will be between 0 to 23 inclusive
     * @param minute will be between 0 to 59 inclusive
     * @param second will be between 0 to 59 inclusive
     * @param millsecond will be 0 to 999 inclusive
     * @param steps will be 0 to 60000 inclusive
     * @param sessionNumber will be 0 to 60000 inclusive
     */
    public void createSessionSession(int hour, int minute, int second, int millsecond, int steps, int sessionNumber){
    
    	int hourClean = Math.abs(hour) % 24;
    	int minuteClean = Math.abs(minute) % 60;
    	int secondClean = Math.abs(second) % 60;
    	int millsecondClean = Math.abs(millsecond) % 1000;
    	
    	String hh = "" + hourClean;
    	String mm = "" + minuteClean;
    	String ss = "" + secondClean;
    	String ms = "" + millsecondClean;
    	String lapTime = hh + ":" + mm + ":" + ss + "." + ms;
    	
    	int stepsClean = Math.abs(steps) % 60001;
    	
    	int sessionClean = Math.abs(sessionNumber) % 60001; 
    	
    	String step = "" + stepsClean;
    	
    	String sessionNo = "" + sessionClean;
    	
    	mDbHelper.createSessionEntry(lapTime, step, sessionNo);
    }
    
    /**
     * Handles screens that display proper tables depending on table type and menu selection
     */
    private void drawTimerScreen(){
    	Cursor dbCursor = mDbHelper.fetchAllTimers();
    	startManagingCursor(dbCursor);
    	String[] columnsFrom = new String[] { _4911MEADbAdapter.KEY_TIMER };
    	int[] dataInto = new int[] { R.id.arow };
    	
    	SimpleCursorAdapter entry = new SimpleCursorAdapter(this, R.layout.row, dbCursor, columnsFrom, dataInto);
    	setListAdapter(entry);
    }
    
    /**
     * Handles screens that display proper tables depending on table type and menu selection
     */
    private void drawPedometerScreen(){
    	Cursor dbCursor = mDbHelper.fetchAllPedometers();
    	startManagingCursor(dbCursor);
    	String[] columnsFrom = new String[] { _4911MEADbAdapter.KEY_PEDOMETER };
    	int[] dataInto = new int[] { R.id.arow };
    	
    	SimpleCursorAdapter entry = new SimpleCursorAdapter(this, R.layout.row, dbCursor, columnsFrom, dataInto);
    	setListAdapter(entry);
    }
    
    /**
     * Handles screens that display proper tables depending on table type and menu selection
     */
    private void drawSessionScreen(){
    	Cursor dbCursor = mDbHelper.fetchAllSessions();
    	startManagingCursor(dbCursor);
    	String[] columnsFrom = new String[] { _4911MEADbAdapter.KEY_SESSION };
    	int[] dataInto = new int[] { R.id.arow };
    	
    	SimpleCursorAdapter entry = new SimpleCursorAdapter(this, R.layout.row, dbCursor, columnsFrom, dataInto);
    	setListAdapter(entry);
    }
}